using System;
using System.Linq.Expressions;
using Volo.Abp.Data;
using Volo.Abp.MultiTenancy;

namespace MatrixFramework.Core.MultiTenancy;

/// <summary>
/// Matrix多租户过滤器接口
/// </summary>
public interface IMatrixTenantFilter
{
    /// <summary>
    /// 应用多租户过滤器到查询表达式
    /// </summary>
    /// <typeparam name="TEntity">实体类型</typeparam>
    /// <param name="query">原始查询表达式</param>
    /// <param name="currentTenantId">当前租户ID</param>
    /// <returns>过滤后的查询表达式</returns>
    Expression<Func<TEntity, bool>> ApplyTenantFilter<TEntity>(Expression<Func<TEntity, bool>> query, Guid? currentTenantId) where TEntity : class, IMultiTenant;

    /// <summary>
    /// 验证实体是否属于当前租户
    /// </summary>
    /// <param name="entity">要验证的实体</param>
    /// <param name="currentTenantId">当前租户ID</param>
    /// <param name="allowHost">是否允许主机租户</param>
    /// <returns>验证结果</returns>
    bool IsValidTenant(IMultiTenant entity, Guid? currentTenantId, bool allowHost = false);

    /// <summary>
    /// 获取实体的租户ID
    /// </summary>
    /// <param name="entity">实体</param>
    /// <returns>租户ID</returns>
    Guid? GetTenantId(IMultiTenant entity);

    /// <summary>
    /// 设置实体的租户ID
    /// </summary>
    /// <param name="entity">实体</param>
    /// <param name="tenantId">租户ID</param>
    void SetTenantId(IMultiTenant entity, Guid? tenantId);

    /// <summary>
    /// 检查是否启用多租户
    /// </summary>
    /// <returns>是否启用</returns>
    bool IsMultiTenantEnabled();

    /// <summary>
    /// 获取数据连接字符串
    /// </summary>
    /// <param name="tenantId">租户ID</param>
    /// <returns>连接字符串</returns>
    string GetConnectionString(Guid? tenantId);

    /// <summary>
    /// 创建租户专用DbContext
    /// </summary>
    /// <param name="tenantId">租户ID</param>
    /// <param name="connectionString">连接字符串</param>
    /// <returns>DbContext</returns>
    IServiceProvider CreateTenantServiceProvider(Guid? tenantId, string connectionString);
}